// var xx = preferences.rulerUnit;

// var layerRef = app.activeDocument.artLayers.getByName("bottom"); 
// layerRef.allLocked = true;



function loadLib() {
    try {
        var loadSuccess = new ExternalObject("lib:\PlugPlugExternalObject"); //载入所需对象，loadSuccess 记录是否成功载入
    }
    catch (e){
        alert(e);// 如果载入失败，输出错误信息
    }

    if (loadSuccess) {

    }
}

function getCurrentBrushInfo() { 

    var brsh = {}; 

    var ref = new ActionReference(); 

    ref.putEnumerated(charIDToTypeID("capp"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt")); 

    var currentBrush = executeActionGet(ref).getObjectValue(stringIDToTypeID("currentToolOptions")).getObjectValue(charIDToTypeID('Brsh')); 

 

    brsh.diameter = currentBrush.getDouble(charIDToTypeID('Dmtr'));

    // 硬毛刷没有某些参数，所以下面都注释掉

    // brsh.hardness = currentBrush.getDouble(charIDToTypeID('Hrdn')); 

    // brsh.angle = currentBrush.getDouble(charIDToTypeID('Angl'));
    //
    // brsh.roundness = currentBrush.getDouble(charIDToTypeID('Rndn'));
    //
    // brsh.spacing = currentBrush.getDouble(charIDToTypeID('Spcn'));

    return brsh
}

function api_getBrushInfo() {
    var eventJAX = new CSXSEvent(); //创建事件对象
    eventJAX.type = "com.mid.event.getBrushInfo"; //设定一个类型名称
    eventJAX.data = getCurrentBrushInfo().diameter;
    eventJAX.dispatch(); // GO ! 发送事件
}


function setCurrentBrushSize(_size) {

    var desc1 = new ActionDescriptor();   

    var ref1 = new ActionReference();   

    ref1.putEnumerated(charIDToTypeID('Brsh'), charIDToTypeID('Ordn'), charIDToTypeID('Trgt'));   

    desc1.putReference(charIDToTypeID('null'), ref1);   

    var desc2 = new ActionDescriptor();   

    desc2.putUnitDouble(stringIDToTypeID("masterDiameter"), charIDToTypeID('#Pxl'), _size);   

    desc1.putObject(charIDToTypeID('T   '), charIDToTypeID('Brsh'), desc2);   

    executeAction(charIDToTypeID('setd'), desc1, DialogModes.NO);   

    return _size;
}

function api_revertHistory() {
    if (activeDocument.activeHistoryState.snapshot)
    {
        var d = new ActionDescriptor();
        var r = new ActionReference();
        r.putEnumerated(stringIDToTypeID("historyState"), stringIDToTypeID("ordinal"), stringIDToTypeID("last"));
        d.putReference(stringIDToTypeID("null"), r);
        executeAction(stringIDToTypeID("select"), d, DialogModes.NO);
    }
    else
    {
        var d = new ActionDescriptor();
        var r = new ActionReference();
        r.putIndex(stringIDToTypeID("snapshotClass"), 1);
        d.putReference(stringIDToTypeID("null"), r);
        executeAction(stringIDToTypeID("select"), d, DialogModes.NO);
    }
}

function api_undo() {
    var docRef = activeDocument;
    var len = docRef.historyStates.length;
    if (len == 0) {
        return;
    }
    // docRef.activeHistoryState = docRef.historyStates[len - 2];
    // return;

    var curIndex = -1;

    for (var i = len - 1; i > 0; i--) {
        if (docRef.historyStates[i] === docRef.activeHistoryState) {
            curIndex = i;
            break;
        }
    }
    if (curIndex > 1) {
        docRef.activeHistoryState = docRef.historyStates[curIndex - 1];
    }

    // 这个不起作用
    // var desc = new ActionDescriptor();
    // var ref = new ActionReference();
    // ref.putEnumerated( charIDToTypeID( "HstS" ), charIDToTypeID( "Ordn" ), charIDToTypeID( "Prvs" ));
    // desc.putReference(charIDToTypeID( "null" ), ref);
    // executeAction( charIDToTypeID( "slct" ), desc, DialogModes.NO );
}

function api_redo() {
    var docRef = activeDocument;
    var len = docRef.historyStates.length;
    if (len == 0) {
        return;
    }
    var curIndex = -1;
    for (var i = len - 1; i > 0; i--) {
        if (docRef.historyStates[i] === docRef.activeHistoryState) {
            curIndex = i;
            break;
        }
    }
    if (curIndex < len - 1) {
        docRef.activeHistoryState = docRef.historyStates[curIndex + 1];
    }
}

// https://forums.adobe.com/thread/579195
function api_getTool(){
    var ref = new ActionReference();
    ref.putEnumerated( charIDToTypeID("capp"), charIDToTypeID("Ordn"), charIDToTypeID("Trgt") );
    var cTool = typeIDToStringID(executeActionGet(ref).getEnumerationType(stringIDToTypeID('tool')));
    return cTool;
}

// https://www.ps-scripts.com/viewtopic.php?f=68&t=11342&p=152772
function api_setTool(tool) {
    var desc9 = new ActionDescriptor();
    var ref7 = new ActionReference();
    ref7.putClass( app.stringIDToTypeID(tool) );
    desc9.putReference( app.charIDToTypeID('null'), ref7 );
    executeAction( app.charIDToTypeID('slct'), desc9, DialogModes.NO );
}
// $.level = 2;
// debugger; 

// brsh = getCurrentBrushInfo();
//
// var _size = brsh.diameter+1;
//
// setCurrentBrushSize(backgroundColor.rgb.blue);

////////////////////////////////////////////////////////////////////////////////////////////////////
// tool names
// Tool names (use quoted strings, e.g. 'moveTool')
// moveTool
// marqueeRectTool
// marqueeEllipTool
// marqueeSingleRowTool
// marqueeSingleColumnTool
// lassoTool
// polySelTool
// magneticLassoTool
// quickSelectTool
// magicWandTool
// cropTool
// sliceTool
// sliceSelectTool
// spotHealingBrushTool
// magicStampTool
// patchSelection
// redEyeTool
// paintbrushTool
// pencilTool
// colorReplacementBrushTool
// cloneStampTool
// patternStampTool
// historyBrushTool
// artBrushTool
// eraserTool
// backgroundEraserTool
// magicEraserTool
// gradientTool
// bucketTool
// blurTool
// sharpenTool
// smudgeTool
// dodgeTool
// burnInTool
// saturationTool
// penTool
// freeformPenTool
// addKnotTool
// deleteKnotTool
// convertKnotTool
// typeCreateOrEditTool
// typeVerticalCreateOrEditTool
// typeCreateMaskTool
// typeVerticalCreateMaskTool
// pathComponentSelectTool
// directSelectTool
// rectangleTool
// roundedRectangleTool
// ellipseTool
// polygonTool
// lineTool
// customShapeTool
// textAnnotTool
// soundAnnotTool
// eyedropperTool
// colorSamplerTool
// rulerTool
// handTool
// zoomTool

